package com.clean.cleantotal.compores.start

import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import com.clean.cleantotal.MainActivity
import com.clean.cleantotal.R
import com.clean.cleantotal.base.BaseActivity
import com.clean.cleantotal.bean.FirstBean
import com.clean.cleantotal.bean.FunType
import com.clean.cleantotal.databinding.ActivityFirstBatteryScanBinding
import com.clean.cleantotal.compores.result.ResultActivity
import com.drake.brv.utils.bindingAdapter
import com.drake.brv.utils.linear
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class FirstBatteryScanActivity : BaseActivity<ActivityFirstBatteryScanBinding>() {
    override val viewBinding: ActivityFirstBatteryScanBinding
        get() = ActivityFirstBatteryScanBinding.inflate(layoutInflater)
    private var allList = ArrayList<FirstBean>()
    override fun initView() {

        mBinding.recyclerView.linear().setup {
            addType<FirstBean> { R.layout.item_battery_scan_layout }
        }

        val intent = ContextCompat.registerReceiver(
            this,
            null,
            IntentFilter(Intent.ACTION_BATTERY_CHANGED),
            ContextCompat.RECEIVER_NOT_EXPORTED
        )
        intent?.let {
            val health = when (it.getIntExtra(BatteryManager.EXTRA_HEALTH, 0)) {
                BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE -> "UNSPECIFIED_FAILURE"
                BatteryManager.BATTERY_HEALTH_DEAD -> "DEAD"
                BatteryManager.BATTERY_HEALTH_OVERHEAT -> "OVERHEAT"
                BatteryManager.BATTERY_HEALTH_GOOD -> "GOOD"
                BatteryManager.BATTERY_HEALTH_COLD -> "COLD"
                BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE -> "OVER VOLTAGE"
                else -> "UNKNOWN"
            }
            val temperature = it.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0)
            val voltage = it.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0)
            val power = it.getIntExtra(BatteryManager.EXTRA_LEVEL, 100)

            val list = listOf(
                FirstBean("Battery Status", health, 0),
                FirstBean("Battery Temp", "${temperature / 10.0}°C", 0),
                FirstBean(
                    "Battery Voltage", "${(voltage / 1000.0).toString().substring(0, 3)}V", 0
                ),
                FirstBean("Battery Power", "$power", 0),
            )
            allList.addAll(list)
            mBinding.recyclerView.models = allList

        }
        lifecycleScope.launch(Dispatchers.IO) {
            updateRecyclerView(allList[0], 1, 0)
            delay(2000)
            updateRecyclerView(allList[0], 2, 0)
            updateRecyclerView(allList[1], 1, 1)
            delay(2000)
            updateRecyclerView(allList[1], 2, 1)
            updateRecyclerView(allList[2], 1, 2)
            delay(2000)
            updateRecyclerView(allList[2], 2, 2)
            updateRecyclerView(allList[3], 1, 3)
            delay(2000)
            updateRecyclerView(allList[3], 2, 3)
            withContext(Dispatchers.Main) {
                openActivity(MainActivity::class.java)
                openActivity(ResultActivity::class.java, Bundle().apply {
                    putSerializable("funType", FunType.BATTERY)
                    putString("title", "Battery")
                })
                finish()
            }
        }
    }

    private fun updateRecyclerView(first: FirstBean, status: Int, position: Int) {
        runOnUiThread {
            first.status = status
            mBinding.recyclerView.bindingAdapter.notifyItemChanged(position, false)
        }
    }

    override fun onBackPressed() {

    }
}